# encoding = utf-8
class CategoriesController < ApplicationController

  before_filter :authenticate_admin!, :only => [:new, :create, :list, :edit, :update, :delete]

  def new
    @category = Category.new
    @action = {:action => 'create'}
    @action_title = '添加新文章类别'
    render :layout => 'admins'
  end

  def create
    category = Category.new(params[:category])

    if category.save
      flash[:success] = '添加文章类别成功'
    else
      flash[:error] = '添加文章类别失败'
    end

    redirect_to :action => "list"
  end

  def list
    @categories = Category.order('created_at desc').paginate :page => params[:page], :per_page => PAGE_SIZE
    render :layout => 'admins'
  end

  def edit
    @category = Category.find(params[:id])
    @action = {:action => 'update', :id => @category.id}
    @action_title = '编辑文章类别'
    render :layout => 'admins', :action => 'new'
  end

  def update
    category = Category.update(params[:id], params[:category])
    if category
      flash[:success] = '文章类别修改成功'
    end
    redirect_to :action => "list"
  end

  def delete
    category = Category.find(params[:id])
    if category.destroy
      flash[:success] = '文章类别删除成功'
    end
    redirect_to :action => "list"
  end
  
end
